** This dofile makes the results related to changes in Mexican and Chinese exports to the United States from 2004 to 2007 caused by the MFA's end

/*
Figure 1: The Effects of the MFA's End on Chinese and Mexican Textile/Apparel Exports to the United States
*/

capture ssc install regsave

** For Figure 1
** China
use "${rawdata}chinatousexports19982011.dta", replace
* Taking the 6 digit from the 4th character of "commoditycode"
gen hs6=substr(commoditycode, 4, 6) 
drop if hs6=="TOTAL" | hs6=="9999AA"
destring hs6, replace 
* Dropping anything that is not 6 digit
drop if hs6<10000 
tostring hs6, replace 
gen hs2=substr(hs6,1,2)
destring hs6, replace
destring hs2, replace
* Keeping only textile and apparel
drop if hs2>63 | hs2<50
drop if period>2010|period<2000
rename period year
rename tradevalue valorendolares
collapse (sum)valorendolares, by(hs6 hs2 year) 
tostring hs6, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
gen mfa_treat=1 if iq_mex_bind80>0.5 & iq_mex_bind80!=.
replace mfa_treat=0 if mfa_treat==.
xi i.year
drop **2004
gen _Iyear_2000=(year==2000)
save "${temp_data}temp.dta", replace

areg valorendolares _I*** if mfa_treat==1, cluster(hs6) absorb(hs6) 
regsave using "${temp_data}coeff_bind.dta", replace ci

use "${temp_data}coeff_bind.dta", clear
keep if index(var, "_Iyear")>0
replace var=subinstr(var, "_Iyear_", "", .)
destring var, gen(year)
set obs 11
replace year = 2004 in 11
replace coef = 0 in 11
replace ci_lower = 0 in 11
replace ci_upper = 0 in 11
sort year
label variable year "Year"
replace coef=coef/1000000
replace ci_lower=ci_lower/1000000
replace ci_upper=ci_upper/1000000
gen binding=1
save "${temp_data}coeff_bind.dta", replace


use "${temp_data}temp.dta", clear
areg valorendolares _I*** if mfa_treat==0, cluster(hs6) absorb(hs6) 
regsave using "${temp_data}coeff_nonbind.dta", replace ci

use "${temp_data}coeff_nonbind.dta", replace
keep if index(var, "_Iyear")>0
replace var=subinstr(var, "_Iyear_", "", .)
destring var, gen(year)
set obs 11
replace year = 2004 in 11
replace coef = 0 in 11
replace ci_lower = 0 in 11
replace ci_upper = 0 in 11
sort year
label variable year "Year"
replace coef=coef/1000000
replace ci_lower=ci_lower/1000000
replace ci_upper=ci_upper/1000000
gen binding=0

append using "${temp_data}coeff_bind.dta"

#delimit ;
twoway (connected coef year if binding==0, lpattern(solid)
yline(0, lwidth(vthin)) xline(2004.5, lwidth(vthin)))
(line ci_upper ci_lower year if binding==0, lpattern (dash dash) color(gs11 gs11))
(connected coef year if binding==1, lpattern(solid)
yline(0, lwidth(vthin)) xline(2004.5, lwidth(vthin)))
(line ci_upper ci_lower year if binding==1, lpattern (dash dash) color(gs7 gs7)),
legend(order(1 "Products With Non-Binding Quota" 4 "Products With Binding Quota")) title(China) scheme(s1mono);
#delimit cr

graph save "${output}mfa_aggregate/china_coef_bind.gph", replace

** Mexico
use "${rawdata}mextousexports19982011.dta", replace
gen hs6=substr(commoditycode, 4, 6) 
drop if hs6=="TOTAL" | hs6=="9999AA"
destring hs6, replace 
drop if hs6<10000 
tostring hs6, replace 
gen hs2=substr(hs6,1,2)
destring hs6, replace
destring hs2, replace
drop if hs2>63 | hs2<50 
drop if period>2010|period<2000
rename period year
rename tradevalue valorendolares
collapse (sum)valorendolares, by(hs6 hs2 year) 
tostring hs6, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
gen mfa_treat=1 if iq_mex_bind80>0.5 & iq_mex_bind80!=.
replace mfa_treat=0 if mfa_treat==.
xi i.year
drop **2004
gen _Iyear_2000=(year==2000)
save "${temp_data}temp.dta", replace

areg valorendolares _I*** if mfa_treat==1, cluster(hs6) absorb(hs6)
regsave using "${temp_data}coeff_bind.dta", replace ci

use "${temp_data}coeff_bind.dta", clear
keep if index(var, "_Iyear")>0
replace var=subinstr(var, "_Iyear_", "", .)
destring var, gen(year)
set obs 11
replace year = 2004 in 11
replace coef = 0 in 11
replace ci_lower = 0 in 11
replace ci_upper = 0 in 11
sort year
label variable year "Year"
replace coef=coef/1000000
replace ci_lower=ci_lower/1000000
replace ci_upper=ci_upper/1000000
gen binding=1
save "${temp_data}coeff_bind.dta", replace

use "${temp_data}temp.dta", replace
areg valorendolares _I*** if mfa_treat==0, cluster(hs6) absorb(hs6)
regsave using "${temp_data}coeff_nobind.dta", replace ci

use "${temp_data}coeff_nobind.dta", clear
keep if index(var, "_Iyear")>0
replace var=subinstr(var, "_Iyear_", "", .)
destring var, gen(year)
set obs 11
replace year = 2004 in 11
replace coef = 0 in 11
replace ci_lower = 0 in 11
replace ci_upper = 0 in 11
sort year
label variable year "Year"
replace coef=coef/1000000
replace ci_lower=ci_lower/1000000
replace ci_upper=ci_upper/1000000
gen binding=0

append using "${temp_data}coeff_bind.dta"

#delimit ;
twoway (connected coef year if binding==0, lpattern(solid)
yline(0, lwidth(vthin)) xline(2004.5, lwidth(vthin)))
(line ci_upper ci_lower year if binding==0, lpattern (dash dash) color(gs11 gs11))
(connected coef year if binding==1, lpattern(solid)
yline(0, lwidth(vthin)) xline(2004.5, lwidth(vthin)))
(line ci_upper ci_lower year if binding==1, lpattern (dash dash) color(gs7 gs7)),
legend(off) title(Mexico) scheme(s1mono);
#delimit cr

graph save "${output}mfa_aggregate/mexico_coef_bind.gph", replace

grc1leg2 ${output}mfa_aggregate/china_coef_bind.gph ${output}mfa_aggregate/mexico_coef_bind.gph, cols(2) xsize(9) graphregion(margin(zero)) iscale(0.9) legendfrom(${output}mfa_aggregate/china_coef_bind.gph)

gr_edit .style.editstyle declared_ysize(5) editcopy
gr_edit .Edit , cmd(.set_rows = 2) cmd(.set_cols =0 ) 
gr_edit .style.editstyle declared_xsize(12) editcopy
gr_edit .style.editstyle aspect_pos(north) editcopy

gr_edit .Edit, cmd(.set_cols = 2) cmd(.set_rows = 0)

gr_edit .plotregion1.graph2.xaxis1.title.style.editstyle size(large) editcopy
gr_edit .plotregion1.graph1.xaxis1.title.style.editstyle size(large) editcopy

gr_edit .legend.Edit, style(labelstyle(size(medlarge)))

gr_edit .style.editstyle boxstyle(shadestyle(color(white))) editcopy


graph export "${output}mfa_aggregate/mfa_effects_on_exports.png", as(png) replace
